package controlLayer;

import java.util.Date;
import java.util.List;

import dbLayer.DBConnection;
import dbLayer.DBStudent;
import dbLayer.DBStudentIF;
import modelLayer.Class;
import modelLayer.Student;

/**
 * This class is a control layer class of the MSAS system. It functions as the link between 
 * our database and the interface. This class is limited to working with the Student class,
 * and whichever other classes student is associated with. There is no direct connection between
 * this class and our database.
 * 
 * @author Danny M�lgaard, Anna Kelemen, Jakob Korsgaard
 * 		   
 * @version 1.3.1
 * 					1.3.1: 		Added more extensive documentation. Verified that
 * 								information contained in the Design Class Diagram,
 * 								is in accordance with the information contained in this
 * 								class.
 * 
 */
public class StudentCtr {

	private DBStudentIF dbStudent;

	public StudentCtr(){
		
		dbStudent = new DBStudent();
	}

	/**
	 * Sends information from the interface on to the database layer, more specifically
	 * helping in the insertion of a new row in the database. Begins a transactions upon
	 * calling the dbLayer method, and rolls it back should anything that could result in dataloss,
	 * or unintended data manipulation occur.
	 * 
	 * @param 	name		String		name of the student
	 * @param 	cpr			String		ssn of the student
	 * @param 	address		String		address of the student
	 * @param 	phone		String		phone number of the student
	 * @param 	email		String		email of the student
	 * @param 	startDate	String		student enrollment date
	 * @param 	sfo			String		determines if the student is attending SFO or not
	 * @param 	c			Class		an instance of the class associated with student
	 * @return 	rc			int			the count of rows changed in the database
	 */
	public int createStudent(String name, String cpr, String address, String phone, String email, 
							 Date startDate, boolean sfo, Class c) {

		int rc = -1;
		try {
			DBConnection.startTransaction();
			rc = dbStudent.insert(name, cpr, address, phone, email, startDate, sfo, c.getId());
			DBConnection.commitTransaction();
		}
		catch (Exception e) {
			DBConnection.rollbackTransaction();
		}
		
		return rc;
		
	}
	
	/**
	 * Sends information from the interface on to the database layer, more specifically
	 * helping in the search of an existing row in the database.
	 * 
	 * @param	name				String			student name.
	 * @param	retrieveAssociation	boolean			determines if we're to retrieve additional info on the student
	 * @return	students			List<Student>	returns the students matching the search.
	 */
	public List<Student> findStudentByName(String name, boolean retrieveAssociation) {

		List<Student> students = dbStudent.find(name, retrieveAssociation);
		if (students != null && students.size() > 0) {
			return students;
		}
		else {
			return null;
		}
		
	}
	
	/**
	 * Sends information from the interface on to the database layer, more specifically
	 * helping in the search of an existing row in the database.
	 * 
	 * @param	id					int				student id.
	 * @param	retrieveAssociation	boolean			determines if we're to retrieve additional info on the student
	 * @throws						Exception		throws SQL exceptions if the query isn't
	 * 												legal.
	 * @return						List<Student>	returns the students matching the search.
	 */
	public Student findStudentById(int id, boolean retrieveAssociation) {

		Student s = dbStudent.findById(id, retrieveAssociation);
		if (s != null) {
			return s;
		}
		else {
			return null;
		}
		
	}
	
	/**
	 * Sends information from the interface on to the database layer, more specifically
	 * helping in the search of all existing rows in the database.
	 * 
	 * @param 	retrieveAssociation	boolean 		determines if we're to retrieve additional info on the student
	 * @return 						List<Student>	a list containing instances of Student 
	 */
	public List<Student> getAll(boolean retrieveAssociation) {

		List<Student> all = dbStudent.getAll(retrieveAssociation);
		if (all != null && all.size() > 0) {
			return all;
		}
		else {
			return null;
		}
		
	}
	
	/**
	 * Sends information from the interface on to the database layer, more specifically
	 * helping in the renewal of an existing row in the database. Begins a transactions upon
	 * calling the dbLayer method, and rolls it back should anything that could result in data loss,
	 * or unintended data manipulation occur.
	 * 
	 * @param 	s		Student		an instance of Student to be updated
	 * @return 	rc		int 		the count of rows changed in the database
	 */
	public int updateStudent(Student s) {
		
		int rc = -1;
		try {
			DBConnection.startTransaction();
			rc = dbStudent.update(s);
			DBConnection.commitTransaction();
		}
		catch (Exception e) {
			DBConnection.rollbackTransaction();
		}
		
		return rc;
	
	}
	
	/**
	 * Sends information from the interface on to the database layer, more specifically
	 * helping in the removal of an existing row in the database. Begins a transactions upon
	 * calling the dbLayer method, and rolls it back should anything that could result in data loss,
	 * or unintended data manipulation occur.
	 * 
	 * @param 	s		Student		an instance of Student to be removed
	 * @return 	rc		int			the count of rows changed in the database
	 */
	public int removeStudent(Student s) {
		
		int rc = -1;
		try {
			DBConnection.startTransaction();
			rc = dbStudent.remove(s.getId());
			DBConnection.commitTransaction();
		}
		catch (Exception e){
			DBConnection.rollbackTransaction();
		}
		
		return rc;
		
	}
	
}
